module WWW
  class Tkd
    class TbExpress < ActiveRecord::Base
      #将taobao获得items转换为Tkd的order对象
      def self.save_from_tabao items,session
        if items['ship']!=nil
          conv = Iconv.new("UTF-8","GB2312")
          items['ship'].each do|item|
            tid = item['tid'][0].strip()
            if tid!=nil
              tb_express = self.find_by_tid tid.to_i
              if tb_express==nil
                tb_express = self.new
              end
              tb_express.tid = item['tid'][0].strip() unless item['tid']==nil
              tb_express.buyer_nick = item['buyer_nick'][0].strip() unless item['buyer_nick']==nil
              tb_express.delivery_start = item['delivery_start'][0].strip() unless item['delivery_start']==nil
              tb_express.delivery_end = item['delivery_end'][0].strip() unless item['delivery_end']==nil
              tb_express.out_sid=item['out_sid'][0].strip() unless item['out_sid']==nil
              tb_express.item_title=item['item_title'][0].strip() unless item['item_title']==nil
              tb_express.receiver_name=item['receiver_name'][0].strip() unless item['receiver_name']==nil
              tb_express.status=item['status'][0].strip()  unless item['status']==nil
              tb_express.express_type=item['type'][0].strip()  unless item['type']==nil
              tb_express.freight_payer=item['freight_payer'][0].strip()   unless item['freight_payer']==nil
              tb_express.seller_confirm=item['seller_confirm'][0].strip()   unless item['seller_confirm']==nil
              tb_express.company_name=item['company_name'][0].strip()   unless item['company_name']==nil
              tb_express.receiver_name=item['receiver_name'][0].strip()   unless item['receiver_name']==nil
              tb_express.created=item['created'][0].strip()   unless item['created']==nil
              tb_express.modified=item['modified'][0].strip()   unless item['modified']==nil
              tb_express.user_id = session[:id]
              tb_express.save
              #将快递信息与订单进行关联
              WWW::Tkd::GwtSellerOrder.save_from_tb_express tb_express
            end
          end
        end
      end
    end
  end
end
